const Pending = "pending"
const Success = "success"
const Reject = "reject"


class myPromise {
  status = Pending
  value = undefined
  resone = undefined
  resolveArr = []
  rejectArr = []
  constructor(fn){
   fn(this.resolve,this.reject)
  }
  resolve = (value)=>{
     if(this.status!==Pending){
         return
     }
     this.status = Success
     this.vaule = value
     this.resolveArr.shift()(this.value)
  }
  reject = (value)=>{
    if(this.status!==Pending){
        return
    }
    this.status = Reject
    this.reason = value
    this.rejectArr.shift()(this.reason)
  }
  then = (successCb,failCb)=>{
      successCb?successCb:value=>value
      failCb?failCb:reason=>reason
      let promise = new myPromise((resolve,reject)=>{
            if(this.status===Success){
                setTimeout(()=>{
                    let x = successCb(this.value)
                    //判断是否链式调用
                    isOwnPromise(x,promise,resolve,reject)
                },0)
            
            }else if(this.status === Reject){
                setTimeout(()=>{
                    let y = failCb(this.reason)
                    isOwnPromise(y,promise,resolve,reject)
                },0)
            
            }else{
              this.resolveArr.push(successCb)
              this.rejectArr.push(failCb)
            }
      })
       return promise

  }
}
function isOwnPromise(fn,promise,resolve,reject){
   if(fn instanceof promise){
    return
   }
    if(fn instanceof myPromise){
        fn.then(esolve,reject)
    }else{
        resolve(fn)
    }
}
let fun1 = new myPromise((resolve)=>{
  return resolve(1)
},(reject)=>{

}).then(val=>{
    console.log(val)
})
